package org.pluslibs.framework.swingplus.utils;

import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import org.apache.commons.lang.StringUtils;

public class BorderUtils {

	public static JComponent setTitledBorder(JComponent component, String panelTitle, int marginX, int marginY) {
		Border border = createTitledBorder(panelTitle, marginX, marginY);
		if ( border!=null ) component.setBorder(border);
		return component;
	}
	public static Border createTitledBorder(String panelTitle, int marginX, int marginY) {
		panelTitle = StringUtils.trimToNull(panelTitle);
		if ( panelTitle==null ) {
			return createEmptyBorder(marginX, marginY);
		} else {
			Border outsideBorder = new TitledBorder(panelTitle);
			Border insideBorder = createEmptyBorder(marginX, marginY);
			if ( insideBorder==null ) {
				return outsideBorder; 
			} else {
				return new CompoundBorder(outsideBorder, insideBorder);
			}
		}
	}
	
	public static JComponent setEmptyBorder(JComponent component, int marginX, int marginY) {
		Border border = createEmptyBorder(marginX, marginY);
		if ( border!=null ) component.setBorder(border);
		return component;
	}
	public static Border createEmptyBorder(int marginX, int marginY) {
		if ( marginX<=0 && marginY<=0 ) {
			return null;
		} else {
			if ( marginX<0 ) marginX = 0;
			if ( marginY<0 ) marginY = 0;
			return new EmptyBorder(marginY, marginX, (marginY>0?marginY+2:0), marginX);
		}
	}
	
}
